//
//  LZLocationManager.m
//  LZBaseDeviceSDK_Example
//
//  Created by panfei mao on 2019/1/7.
//  Copyright © 2019 panfei mao. All rights reserved.
//

#import "LZLocationManager.h"
#import "LZAuthorizeManager.h"

@interface LZLocationManager () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager            *locationManager;

@property (nonatomic, copy) DidGetGeolocationsCompledBlock didGetGeolocationsCompledBlock;

@end

@implementation LZLocationManager

+ (LZLocationManager *)shareLocationManager{
    static LZLocationManager *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[LZLocationManager alloc] init];
    });
    return instance;
}

- (void)setup {
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    _locationManager.distanceFilter = 10.0;
    [_locationManager requestWhenInUseAuthorization];
    // 开启定位
    if (([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways))) {
        [self.locationManager startUpdatingLocation];
    } else {
        //  打开地理位置定位弹窗
        [LZAuthorizeManager authorizeGeographicLocation];
    }
}

- (id)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)dealloc {
    self.locationManager.delegate = nil;
    self.locationManager = nil;
    self.didGetGeolocationsCompledBlock = nil;
}

- (void)getCurrentGeolocationsCompled:(DidGetGeolocationsCompledBlock)compled {
    self.didGetGeolocationsCompledBlock = compled;
    if (([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways))) {
        [self.locationManager startUpdatingLocation];
    } else {
        //  打开地理位置定位弹窗
        [LZAuthorizeManager authorizeGeographicLocation];
    }
}

#pragma mark - CLLocationManager Delegate
//  代理方法实现
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:
     ^(NSArray* placemarks, NSError* error) {
         if (self.didGetGeolocationsCompledBlock) {
             self.didGetGeolocationsCompledBlock(placemarks);
         }
         if (placemarks.count > 0) {
             CLPlacemark *placeMark = placemarks[0];
             CLLocation *location = placeMark.location;
             CLLocationCoordinate2D coordinate = location.coordinate;
             //  经度
             NSString *longitude = [NSString stringWithFormat:@"%.6f",coordinate.longitude];
             [LZLocationManager shareLocationManager].longitudeString = longitude;
             //  纬度
             NSString *latitude = [NSString stringWithFormat:@"%.6f",coordinate.latitude];
             [LZLocationManager shareLocationManager].latitudeString = latitude;
         }
     }];
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [manager stopUpdatingLocation];
}

@end
